class Solution:
    def merge(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: List[Interval]
        """

        lista = []
        listb = []

        aa = []
        bb = []
        for a, b in intervals:
            lista.append(a)
            listb.append(b)
            lista.sort()
            listb.sort()
        aa.append(lista[0])
        for i in range(1, len(intervals)):
            if lista[i] > listb[i - 1]:
                aa.append(lista[i])
                bb.append(listb[i - 1])
        bb.append(listb[len(intervals) - 1])

        endlist = []
        for i in zip(aa, bb):
            endlist.append(list(i))
        return endlist


ab = [[1, 3], [2, 6], [8, 10], [5, 18]]
s = Solution()
endlist = s.merge(ab)
print(endlist)
